#include<bits/stdc++.h>
#define N 2500
#define int long long
using namespace std;
int n, root;
int l[N], r[N], fa[N];
int L[N], R[N], Fa[N], dep[N], ans[N];
void xuan(int x) {
    int f = fa[x], ls = l[x], rs = r[x];
    if(l[f] == x) {
        l[f] = rs;
        r[x] = f;
        l[x] = fa[f];
        fa[rs] = f;
        fa[f] = x;
        fa[fa[f]] = x;
	}
	else if(r[f] == x) {
	    r[f] = ls;
	    l[x] = f;
	    r[x] = fa[f];
	    fa[ls] = f;
	    fa[f] = x;
	    fa[fa[f]] = x;
	}
}
void splay(int x) {
    int f = fa[x], ls = l[x], rs = r[x];
    if(f == root) xuan(x);
    else if(l[f] == x && l[fa[f]] == f) {
        xuan(f);
        xuan(x);
	}
	else {
	    xuan(x);
	    xuan(x);
	}
}
void dfs(int x, int fa) {
    dep[x] = dep[fa] + 1;
    if(l[x]) {
        dfs(l[x], x);
	} 
    if(r[x]) {
        dfs(r[x], x);
	}
    ans[x] += dep[x];
    //cout << x << ":" << dep[x] << endl;
    return;
}
signed main() {
    freopen("dream.in", "r", stdin);
    freopen("dream.out", "w", stdout);
	cin >> n;
	for(int i = 1; i <= n; i++) {
	    cin >> L[i] >> R[i];
	    Fa[L[i]] = Fa[R[i]] = i;
	}
	root = 1;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            l[j] = L[j];
            r[j] = R[j];
            fa[j] = Fa[j];
		}
		splay(i);
		memset(dep, 0, sizeof(dep));
		dep[0] = -1;
		dfs(i, 0);
	}
	for(int i = 1; i <= n; i++) cout << ans[i] << endl;
	return 0;
}
